package com.jarchie.htgl.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.jarchie.htgl.R;
import com.jarchie.htgl.animation.ExpandableViewHoldersUtil;
import com.jarchie.htgl.animation.ExpandableViewHoldersUtil.KeepOneHolder;
import com.jarchie.htgl.bean.ApproveListBean;
import java.util.List;

/**
 * Created by Jarchie on 2017\12\27.
 * RecyclerView列表的适配器(可分组多级列表适配器)
 */

public class MyListAdapter extends RecyclerView.Adapter<MyListAdapter.ViewHolder> {
    KeepOneHolder<ViewHolder> keepOne = new KeepOneHolder<>();
    private Context mContext;
    private List<ApproveListBean.INFOBean> mList;
    //事件回调的监听
    private MyListAdapter.OnItemClickListener onItemClickListener;

    public MyListAdapter(Context context, List<ApproveListBean.INFOBean> list) {
        this.mContext = context;
        this.mList = list;
    }

    //设置回调监听
    public void setOnItemClickListener(MyListAdapter.OnItemClickListener listener) {
        this.onItemClickListener = listener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(mContext).inflate(R.layout.adapter_recycler_list, parent, false));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.bind(position, mList.get(position));
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, ExpandableViewHoldersUtil.Expandable {
        private int currentPos = 0;
        TextView contractNum, contractName, companyName;
        LinearLayout containLayout, underLayout, imgLayout;
        ImageView mImageView;
        TextView startTime, endTime, agent, amount, departName, clientName;

        public ViewHolder(View itemView) {
            super(itemView);
            containLayout = itemView.findViewById(R.id.contain_layout);
            underLayout = itemView.findViewById(R.id.layout_under);
            imgLayout = itemView.findViewById(R.id.img_layout);
            contractNum = itemView.findViewById(R.id.tv_contract_number);
            contractName = itemView.findViewById(R.id.tv_contract_name);
            companyName = itemView.findViewById(R.id.tv_company_name);
            mImageView = itemView.findViewById(R.id.rotate_view);
            startTime = itemView.findViewById(R.id.tv_start_time);
            endTime = itemView.findViewById(R.id.tv_end_time);
            agent = itemView.findViewById(R.id.tv_agent);
            amount = itemView.findViewById(R.id.tv_amount);
            departName = itemView.findViewById(R.id.tv_departmt_name);
            clientName = itemView.findViewById(R.id.tv_client_name);
            imgLayout.setOnClickListener(this);
            containLayout.setOnClickListener(this);
        }

        public void bind(int pos, ApproveListBean.INFOBean bean) {
            currentPos = pos; //全局变量，获取下标位置
            contractNum.setText(bean.getHtbh()); //合同编号
            contractName.setText(bean.getHtmc()); //合同名称
            companyName.setText(bean.getGsmc()); //公司名称
            startTime.setText(bean.getQsrq()); //起始日期
            endTime.setText(bean.getZzrq()); //终止日期
            agent.setText(bean.getJbr()); //经办人
            amount.setText("¥ " + bean.getJe()); //金额
            departName.setText(bean.getBmmc()); //部门名称
            clientName.setText(bean.getKhmc()); //客户名称
            keepOne.bind(this, pos);
        }

        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.img_layout:
                    keepOne.toggle(this, mImageView);
                    break;
                case R.id.contain_layout:
                    if (onItemClickListener != null) {
                        onItemClickListener.onItemClick(currentPos);
                    }
                    break;
            }
        }

        @Override
        public View getExpandView() {
            return underLayout;
        }
    }

    public interface OnItemClickListener {
        void onItemClick(int position);
    }

}
